﻿#define _CRT_SECURE_NO_WARNINGS
#include"snake.h"

//设置光标位置
void SetPos(short x, short y)
{
	COORD pos = { x,y };
	HANDLE hOutput = NULL;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput, pos);
}

//游戏暂停
void pause()
{
	while (1)
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

//游戏欢迎界面
void WelcomeToGame()
{
	SetPos(40, 25);
	system("pause");
	system("cls");

	SetPos(25, 12);
	printf("用 ↑ . ↓ . ← . → 分别控制蛇的移动， F3为加速，F4为减速\n");
	//直接复制pdf的话'用'会输出?
	SetPos(25, 13);
	printf("加速可获得更高分数\n");
	SetPos(40, 25);
	system("pause");
	system("cls");
}

//打印地图
void CreatMap()
{
	int i = 0;

	//打印上边(0,0)-(56,0)
	SetPos(0, 0);
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%c", WALL);
	}

	//打印下边(0,26)-(56,26)
	SetPos(0, 26);
	for (i = 0; i < 58; i += 2)
	{
		wprintf(L"%c", WALL);
	}

	//打印左边
	for (i = 1; i < 26; i ++)
	{
		SetPos(0, i);
		wprintf(L"%c", WALL);
	}

	//打印右边
	for (i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%c", WALL);
	}
}

//初始化蛇
void InitSnake(pSnake ps)
{
	pSnakeNode cur = NULL;
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		cur->next = NULL;
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;

		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}
	//遍历链表打印蛇身
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	//初始化蛇的一些数据
	ps->_SleepTime = 200;
	ps->_Score = 0;
	ps->_Status = OK;
	ps->_Dir = RIGHT;
	ps->_Add = 10;
}

//创建食物
void CreateFood(pSnake ps)
{
	int x = 0, y = 0;

again://坐标与蛇身重合时，goto到此处重新生成坐标
	do 
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x%2!=0);

	//遍历蛇身看生成的食物坐标是否与蛇重合
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto  again;
		}
		cur = cur->next;
	}

	//不与蛇身重合，创建食物
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreateFood()::malloc()");
		return;
	}
	else
	{
		pFood->x = x;
		pFood->y = y;
		SetPos(pFood->x, pFood->y);
		wprintf(L"%c", FOOD);
		ps->_pFood = pFood;
	}
}

//打印帮助信息
void PrintHelpInfo()
{
	//打印提⽰信息
	SetPos(64, 15);
	printf("不能穿墙，不能咬到自己\n");
	SetPos(64, 16);
	printf("用↑.↓.←.→分别控制蛇的移动");
	SetPos(64, 17);
	printf("F1 为加速，F2 为减速\n");
	SetPos(64, 18);
	printf("ESC ：退出游戏.space：暂停游戏.");
	SetPos(64, 20);
	printf("⽐特就业课@版权");
}

//判断下一个节点是否是食物
int NextIsFood(pSnakeNode psn, pSnake ps) 
{
	return (psn->x == ps->_pFood->x) && (psn->y == ps->_pFood->y);
}

//吃食物，蛇变长
void EatFood(pSnakeNode psn, pSnake ps)
{
	psn->next = ps->_pSnake;
	ps->_pSnake = psn;

	//遍历链表打印蛇
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	ps->_Score += ps->_Add;//吃食物加分

	free(ps->_pFood);//释放食物，重新创建
	CreateFood(ps);
}

//不吃食物，继续移动
void NoFood(pSnakeNode psn, pSnake ps)
{
	//头插法
	psn->next = ps->_pSnake;
	ps->_pSnake = psn;

	//遍历链表打印蛇(最后一个节点不打印)
	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	//最后⼀个位置打印空格，然后释放节点
	SetPos(cur->next->x, cur->next->y);
	printf("  ");//使用两个空格覆盖蛇尾
	free(cur->next);
	cur->next = NULL;
}

//原版本不打印倒数第二个节点，此版本无法正常运行
//void NoFood(pSnakeNode psn, pSnake ps)
//{
//	//头插法
//	psn->next = ps->_pSnake;
//	ps->_pSnake = psn;
//
//	//遍历链表打印蛇(最后一个节点不打印)
//	pSnakeNode cur = ps->_pSnake;
//	while (cur->next)//不会
//	{
//		SetPos(cur->x, cur->y);
//		wprintf(L"%c", BODY);
//		cur = cur->next;
//	}
//	//最后⼀个位置打印空格，然后释放节点
//	SetPos(cur->x, cur->y);
//	printf("  ");//使用两个空格覆盖蛇尾
//	free(cur);
//	cur = NULL;
//}

//检测撞墙
int KillByWall(pSnake ps)
{
	if ((ps->_pSnake->x == 0)
		|| (ps->_pSnake->x == 56)
		|| (ps->_pSnake->y == 0)
		|| (ps->_pSnake->y == 26))
	{
		ps->_Status = KILL_BY_WALL;
		return 1;
	}
	return 0;
}

//检测撞自己
int KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if ((ps->_pSnake->x == cur->x) && (ps->_pSnake->y == cur->y))
		{
			ps->_Status = KILL_BY_SELF;
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}

//游戏开始前，进行初始化
void Gamestart(pSnake ps)
{
	//设置控制台窗口大小
	system("mode con cols=100 lines=30");
	//也可写为system("mode 100,30");
	system("title 贪吃蛇");

	//获取标准输出句柄，标识输入设备
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

	//隐藏光标
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput,&CursorInfo);//获取光标信息
	CursorInfo.bVisible = false;//隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态


	//打印欢迎界面
	WelcomeToGame();

	//打印地图
	CreatMap();

	//初始化蛇
	InitSnake(ps);

	//创建第一个食物想·
	CreateFood(ps);
}


//蛇身移动
void SnakeMove(pSnake ps)
{
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	switch (ps->_Dir)
	{
		case UP:
		{
			pNextNode->x = ps->_pSnake->x;
			pNextNode->y = ps->_pSnake->y-1;
		}
			break;
		case DOWN:
		{
			pNextNode->x = ps->_pSnake->x;
			pNextNode->y = ps->_pSnake->y+1;
		}
			break;
		case LEFT:
		{
			pNextNode->x = ps->_pSnake->x - 2;
			pNextNode->y = ps->_pSnake->y;
		}
			break;
		case RIGHT:
		{
			pNextNode->x = ps->_pSnake->x + 2;
			pNextNode->y = ps->_pSnake->y;
		}
			break;
	}

	if (NextIsFood(pNextNode, ps))
	{
		EatFood(pNextNode, ps);
	}
	else
	{
		NoFood(pNextNode, ps);
	}

	//撞墙检测
	KillByWall(ps);
	//咬自己检测
	KillBySelf(ps);
}

//运行游戏
void GameRun(pSnake ps)
{
	PrintHelpInfo();//打印帮助信息
	do
	{
		SetPos(64, 10);
		printf("得分：%d", ps->_Score);
		printf("每个食物得分：%d", ps->_Add);

		//上下左右
		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)
		{
			ps->_Dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_Dir != UP)
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)
		{
			ps->_Dir = RIGHT;
		}

		//暂停、退出
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_Status = END_NOMAL;
			break;
		}

		//加速、减速
		else if (KEY_PRESS(VK_F2))
		{
			ps->_SleepTime -= 30;
			ps->_Add += 1;
		}
		else if (KEY_PRESS(VK_F3))
		{
			ps->_SleepTime += 30;
			ps->_Add -= 1;
			if (ps->_SleepTime >= 350)
			{
				ps->_Add = 1;
			}
		}

		Sleep(ps->_SleepTime);
		SnakeMove(ps);


	}while (ps->_Status==OK);
}

//游戏结束
void GameEnd(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake;
	SetPos(24, 12);
	switch (ps->_Status)
	{
	case END_NOMAL:
		printf("主动退出\n");
		break;
	case KILL_BY_SELF:
		printf("背后中枪，是自杀\n");
		break;
	case KILL_BY_WALL:
		printf("死于撞墙\n");
		break;
	}

	//释放蛇身节点
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}